﻿using FuelDXApplication1.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FuelDXApplication1.TmzWinform
{
    public partial class AlreadyReport : Form
    {
        public AlreadyReport()
        {
            InitializeComponent();
        }

        private void AlreadyReport_Load(object sender, EventArgs e)
        {
            // 使用 DbHelper 获取数据
            var dataTable = DBHelper.Query<BasicInformation>();
            gridControl1.DataSource = dataTable.Result;
            //绑定燃料类型
            GetFuelType();
            //绑定车辆类型
            GetVehicleTypeInFo();
        }



        /// <summary>
        /// 绑定燃料类型下拉
        /// </summary>
        public async void GetFuelType()
        {
            var fuelType = await DBHelper.Query<FuelType>();
            this.lookUpEdit1.Properties.DataSource = fuelType;
            this.lookUpEdit1.Properties.ValueMember = "FuelTypeId";
            this.lookUpEdit1.Properties.DisplayMember = "FuelTypeName";
        }
        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        public async void GetVehicleTypeInFo()
        {
            var vehicleTypeInFo = await DBHelper.Query<VehicleTypeInFo>();
            this.lookUpEdit2.Properties.DataSource = vehicleTypeInFo;
            this.lookUpEdit2.Properties.ValueMember = "VehicleTypeId";
            this.lookUpEdit2.Properties.DisplayMember = "VehicleTypeName";
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            string iCPRecordNumber = textEdit1.Text;
            string vehicleModel = textEdit2.Text;
            var selecttext = lookUpEdit1.EditValue;
            var versionId = lookUpEdit2.EditValue;
            var sdate = dateEdit1.EditValue;
            var edate = dateEdit2.EditValue;
            var list = DBHelper.Query<BasicInformation>();
            if (list.Result != null)
            {
                // 使用多个条件进行筛选
                var filterdata = list.Result.AsQueryable();

                // 根据ICPRecordNumber（iCPRecordNumber）筛选
                if (!string.IsNullOrEmpty(iCPRecordNumber))
                {
                    filterdata = filterdata.Where(b => b.ICPRecordNumber == iCPRecordNumber);
                }

                // 根据VehicleModel（vehicleModel）筛选
                if (!string.IsNullOrEmpty(vehicleModel))
                {
                    filterdata = filterdata.Where(b => b.VehicleModel == vehicleModel);
                }
                //// 根据VersionId（text）筛选
                //if (!string.IsNullOrEmpty(text) && int.TryParse(text, out int versionId))
                //{
                //    filterdata = filterdata.Where(b => b.VersionId == versionId);
                //}
                // 根据comboBoxEdit1的选择进行筛选（如果需要的话）
                if (selecttext != null)
                {
                    filterdata = filterdata.Where(b => b.FuelTypeId == Convert.ToInt32(selecttext));
                }
                if (versionId != null)
                {
                    filterdata = filterdata.Where(b => b.VersionId == Convert.ToInt32(versionId));
                }
                if (sdate != null)
                {
                    filterdata = filterdata.Where(b => b.DateOfImport >= Convert.ToDateTime(sdate));
                }
                if (edate != null)
                {
                    filterdata = filterdata.Where(b => b.DateOfImport <= Convert.ToDateTime(edate));
                }
                // 更新Grid数据源
                gridControl1.DataSource = filterdata.ToList();
            }
            else
            {
                // 如果没有数据，清空 gridControl
                gridControl1.DataSource = null;
            }
        }
    }
}
